@echo off

call :getsectime
REM create log file if it doesn't exist already, then add a new section
set run1fsplog=run1fsp.log
if not exist %run1fsplog% touch %run1fsplog%
echo -------------------------------------------------------------------------------- >> %run1fsplog%
echo New run1fsp job starting at %time% on %date% >> %run1fsplog%

if "%1"=="" goto :usage

REM email address specified
if "%1" == "-e" (
	if "%2"=="" ( 
		echo Error: missing email address!
		echo Error: missing email address! >> %run1fsplog%
		echo -------------------------------------------------------------------------------- >> %run1fsplog%
		echo.
		goto :usage
	)
	if "%3"=="" ( 
		echo Error: missing file!
		echo Error: missing file! >> %run1fsplog%
		echo arguments supplied were: >> %run1fsplog%
		echo %%1=%1 >> %run1fsplog%
		echo %%2=%2 >> %run1fsplog%
		echo %%3=%3 >> %run1fsplog%
		echo -------------------------------------------------------------------------------- >> %run1fsplog%
		echo.
		goto :usage
	)
	REM test to see if there's an @ symbol (simple check to see if it's an email address)
	set testval=%2
	REM remove anything before @
	set testval=%testval:*@=%
	REM limit testval to length 1
	set testval=%testval:~0,1%
	if not "%testval%"=="@" ( 
		echo Error: missing @ in email address!
		echo Error: missing @ in email address! >> %run1fsplog%
		echo -------------------------------------------------------------------------------- >> %run1fsplog%
		echo.
		goto :usage
	)
	REM set email to address
	set to=%2
	REM get rid of email arguments
	shift
	shift
)

set _params=%*
call :namesetter "%_params%"
REM add job-start to log
echo Starting job on file %filename%, located in directory >> %run1fsplog%
echo %filepath% >> %run1fsplog%



REM pw for gmail account
set pw=beaming2009					
REM username & from address for gmail account
set from=masimulation1@gmail.com	
REM default to: address
set to=jcb91@cam.ac.uk				


REM determines the log file name - _p0 is added because it's the log from
::  process 0. I guess you could also add logs from other processes as
::  attachments, but they're only necessary if something's gone very wrong
::  anyway.
set logfile=%filename:.fsp=%_p0.log	
REM server for blat. This server is the STunnel listener for encryption.
set serv=127.0.0.1
REM subject line for success email
set successmsg=Simulation '%filename%' has completed successfully
REM subject line for failure email
set failmsg=Simulation '%filename%' has ended unexpectedly!

REM copy file to uncompleted location
:: set compfile=%date:~6,4%%date:~3,2%%date:~0,2%_completed
::if not exist "%compfile%" md "%compfile%" > NUL
::copy "%filename%" "u_%filename%" > NUL

call :getsectime
set /a starttime=sectime > NUL
echo starting job at %time% on %date% >> %run1fsplog%
echo on
"C:\Program Files\Lumerical\FDTD\bin\..\mpich2\mpiexec" -channel nemesis -n 8 -binding user:4,7,6,5,0,3,2,1 "C:\Program Files\Lumerical\FDTD\bin\FDTD-par" -fullinfo -logall "'%1'"
@echo off

REM %errorlevel% is set by the previous command.
if %errorlevel% == 0 goto :success


: failure
echo job failure at %time% on %date% >> %run1fsplog%
echo emailing logs to %to% >> %run1fsplog%
REM email failure notice with logfile, or if there's an error (probably opening
::  the log), email without it.
blat %logfile% -server %serv% -f %from% -u %from% -pw %pw% -to %to% -subject "%failmsg%" -q || ^
blat -body "log file unavailable!" -server %serv% -f %from% -u %from% -pw %pw% -to %to% -subject "%failmsg%"  >> %run1fsplog%
REM exit code so a multiple-job runner knows it's failed
echo exiting with error code 1 >> %run1fsplog%
echo -------------------------------------------------------------------------------- >> %run1fsplog%
echo.
exit /B 1


: success
echo finished job at %time% on %date% >> %run1fsplog%
REM calculate the time taken (assuming no days!)
call :getsectime
set /a diff=sectime-starttime > NUL
call :gethrmnsctime %diff%
echo job took %hr% hours, %mn% minutes and %sc% seconds
REM email success notice with logfile
blat %logfile% -server %serv% -f %from% -u %from% -pw %pw% -to %to% -subject "%successmsg% in %hr%:%mn%:%sc%" >> %run1fsplog%
REM delete p1-7 log files, pausing first to allow them to be left alone by the fdtd-par process
ping -n 3 127.0.0.1>nul
del %filename:.fsp=%_p1.log
del %filename:.fsp=%_p2.log
del %filename:.fsp=%_p3.log
del %filename:.fsp=%_p4.log
del %filename:.fsp=%_p5.log
del %filename:.fsp=%_p6.log
del %filename:.fsp=%_p7.log
REM exit code so a multiple-job runner knows it's succeeded
echo exiting with 0 error code >> %run1fsplog%
echo -------------------------------------------------------------------------------- >> %run1fsplog%
echo.
start/min mplay32 /play /close %windir%\media\ding.wav
exit /B 0

: usage
echo This batch script will execute a single .fsp file in parallel mode on 8 cores,
echo then email the result to a specified address (defaults to jcb91@cam.ac.uk).
echo syntax:
echo     run1fsp (-e [email]) [filename]
echo        runs the fsp file [filename], notifying email address [email] if
echo        specified (otherwise default email is used). Filenames with spaces
echo        should be enclosed in double quotes
goto :eof

:namesetter
:: just set names
REM gets the drive letter & path
set filepath=%~d1%~p1				
REM gets the unqualified filename
set filename=%~n1%~x1
goto :eof

:getsectime
set year=%date:~6,4%
set month=%date:~3,2%
set day=%date:~0,2%
set hr=%time:~0,2%
set mn=%time:~3,2%
set sc=%time:~6,2%
set /a sectime=day*86400+hr*3600+mn*60+sc > NUL
goto :eof

:gethrmnsctime
set _var=%1
echo gethrminsec %_var%
set /a day=_var/86400      > NUL
set /a _var=_var%86400 > NUL 
set /a hr=_var/3600        > NUL
set /a _var=_var%3600   > NUL
set /a mn=_var/60 > NUL
set /a _var=_var%60 > NUL
set /a sc=_var > NUL
goto :eof